//
//  MessageViewController.m
//  OperationPro
//
//  Created by yaxue wang on 16/11/3.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "MessageViewController.h"
#import "SystemMessageViewController.h"
#import "MainMessageCell.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"
#import "StateChangeViewController.h"
#import "ContenxtMessageViewController.h"
#import "DicuessMessageViewController.h"
#import "HomeViewController.h"
#import "EmergencySignViewController.h"

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray*dataAr;
@property(nonatomic,strong)NSMutableDictionary*systemDic;
@property(nonatomic,strong)NSMutableDictionary*taskmDic;
@property(nonatomic,strong)NSMutableDictionary*contextDic;
@property(nonatomic,strong)NSMutableDictionary*dicuessDic;
@property(nonatomic,strong)NSMutableDictionary*emergencyDic;
@property(nonatomic,strong)UIView *ringView;
@property(nonatomic,strong)UILabel *rightLab;

@end

@implementation MessageViewController
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self requestData];
}
- (void)initView
{
    [super initView];
    self.dataAr=[[NSMutableArray alloc]init];
    self.navBar.hidden=YES;
    self.navBar.leftView=nil;
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor =[UIColor clearColor];
    [self.view addSubview:narView];
    [narView resignFirstResponder];
    __block typeof(self) blockself = self;
    UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 64)];
    [narView addSubview:backView];
    [backView bk_whenTapped:^{
        
    if([blockself.type isEqualToString:@"推送"]) {
        HomeViewController *homeVC=[[HomeViewController alloc]init];
        [blockself.navigationController pushViewController:homeVC animated:NO];
    }else  {
        [blockself backController];
                
        
    }
       
    }];
    
    UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_back"];
    UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(15, 33, icoBack.size.width, icoBack.size.height)];
    imgBack.image=icoBack;
    [backView addSubview:imgBack];
    
    UILabel *backTitle = [[UILabel alloc] init];
    backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 20, 50 - imgBack.frame.size.width,44);
    
    [backTitle setText:@"返回"];
    [backTitle setTextColor:MainColor];
    [backTitle setFont:F(14)];
    [backView addSubview:backTitle];
    
    UILabel *titleLab =[[UILabel alloc]init];
    [narView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(narView);
        make.top.equalTo(narView).offset(30);
    }];
    titleLab.text=@"消息通知";
    titleLab.font =F(16);
    titleLab.textColor =C(@"5a5a5a");
    
    self.ringView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-85, 0, 80, 70)];
    [narView addSubview:self.ringView];
    self.rightLab =[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 70, 44)];
    self.rightLab.text =@"全部已读";
    self.rightLab.textAlignment=NSTextAlignmentRight;
    self.rightLab.font=F(14);
    self.rightLab.tag=200;
    self.rightLab.userInteractionEnabled=YES;
    [self.ringView addSubview: self.rightLab];
    
    [self.ringView bk_whenTapped:^{
        
        self.isLoading=YES;
        [CClass HttpRequestTask:YES withUrl:@"message_doAllRead" withData:nil notifyError:YES onSuccess:^(BaseModel *model) {
            //对表格进行刷新
            
            self.isLoading=NO;
            [self requestData];
            
        } onError:^(BaseModel *model) {
            self.isLoading=NO;

        }];
        
    }];
    
    self.taskmDic=[[NSMutableDictionary alloc]init];
    self.contextDic=[[NSMutableDictionary alloc]init];
    self.systemDic=[[NSMutableDictionary alloc]init];
    self.dicuessDic=[[NSMutableDictionary alloc]init];
    self.emergencyDic =[[NSMutableDictionary alloc]init];
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth, 65*4)];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.backgroundColor=[UIColor whiteColor];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator =NO;
    [self.tableView registerClass:[MainMessageCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    self.tableView.scrollEnabled=NO;
    [self requestData];
    

}
- (void)requestData
{
    [CClass HttpRequestTask:YES withUrl:@"message_showAllNotify" withData:nil notifyError:YES onSuccess:^(BaseModel *model) {
        self.systemDic=[model.B_data objectForKey:@"systemNotify"];
        self.taskmDic=[model.B_data objectForKey:@"taskNotify"];
        self.contextDic=[model.B_data objectForKey:@"replyNotify"];
        self.dicuessDic=[model.B_data objectForKey:@"discussionNotify"];
        self.emergencyDic =[model.B_data objectForKey:@"emergencyNotify"];
        NSInteger messageNum;
        messageNum=[[self.systemDic objectForKey:@"cnt"]integerValue]+[[self.taskmDic objectForKey:@"cnt"]integerValue]+[[self.contextDic objectForKey:@"cnt"] integerValue]+[[self.dicuessDic objectForKey:@"cnt"] integerValue]+[[self.emergencyDic objectForKey:@"cnt"]integerValue];
        if (messageNum==0) {
            
            self.ringView.userInteractionEnabled=NO;
            self.rightLab.textColor=[UIColor grayColor];
        }else
        {
            self.ringView.userInteractionEnabled=YES;
            self.rightLab.textColor=MainColor;
            
        }
        self.isLoading=NO;
        [self.tableView reloadData];
    } onError:^(BaseModel *model) {
        self.isLoading=NO;

    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 4;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 65;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MainMessageCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[MainMessageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        
    }
    
    if (indexPath.row==0) {
        cell.headImg.image=[UIImage imageNamed:@"Ico_mess_noti"];
        cell.nameLab.text = @"系统通知";
        if ([[self.systemDic objectForKey:@"cnt"]integerValue]==0) {
            cell.redLab.backgroundColor=[UIColor clearColor];
            
        }else
        {
            cell.redLab.backgroundColor=[UIColor redColor];
            
        }
        cell.timeLab.text=[self.systemDic objectForKey:@"created"];
        cell.contentLab.text=[self.systemDic objectForKey:@"title"];
    }else if(indexPath.row==1)
    {
        cell.headImg.image=[UIImage imageNamed:@"Ico_mess_change"];
        cell.nameLab.text = @"状态变更";
        if ([[self.taskmDic objectForKey:@"cnt"]integerValue]==0) {
            cell.redLab.backgroundColor=[UIColor clearColor];
            
        }else
        {
            cell.redLab.backgroundColor=[UIColor redColor];
            
        }
        cell.timeLab.text=[self.taskmDic objectForKey:@"created"];
        cell.contentLab.text=[self.taskmDic objectForKey:@"title"];
        
    }else if(indexPath.row==2)
    {
        cell.headImg.image=[UIImage imageNamed:@"Ico_mess_context"];
        cell.nameLab.text = @"任务回复";
        if ([[self.contextDic objectForKey:@"cnt"]integerValue]==0) {
            cell.redLab.backgroundColor=[UIColor clearColor];
            
        }else
        {
            cell.redLab.backgroundColor=[UIColor redColor];
            
        }
        cell.timeLab.text=[self.contextDic objectForKey:@"created"];
        cell.contentLab.text=[self.contextDic objectForKey:@"title"];
    }else if(indexPath.row==3)
    {
        
        cell.headImg.image=[UIImage imageNamed:@"Ico_mess_dicuss"];
        cell.nameLab.text = @"讨论贴回复";
        if ([[self.dicuessDic objectForKey:@"cnt"]integerValue]==0) {
            cell.redLab.backgroundColor=[UIColor clearColor];
            
        }else
        {
            cell.redLab.backgroundColor=[UIColor redColor];
            
        }
        cell.timeLab.text=[self.dicuessDic objectForKey:@"created"];
        cell.contentLab.text=[self.dicuessDic objectForKey:@"title"];

    }else if(indexPath.row==4)
    {
        cell.headImg.image=[UIImage imageNamed:@"ico_mess_em"];
        cell.nameLab.text = @"应急签到";
        if ([[self.emergencyDic objectForKey:@"cnt"]integerValue]==0) {
            cell.redLab.backgroundColor=[UIColor clearColor];
            
        }else
        {
            cell.redLab.backgroundColor=[UIColor redColor];
            
        }
        cell.timeLab.text=[self.emergencyDic objectForKey:@"created"];
        cell.contentLab.text=[self.emergencyDic objectForKey:@"title"];
    }
        
        
        
    
    return cell;
    
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    if (indexPath.row==0) {
        SystemMessageViewController *systemVC=[[SystemMessageViewController alloc]init];
        [self pushController:systemVC];
    }else if(indexPath.row==1)
    {
        StateChangeViewController *stateVC=[[StateChangeViewController alloc]init];
        [self pushController:stateVC];
    }else if (indexPath.row==2){
        
        ContenxtMessageViewController *contextMessageVC=[[ContenxtMessageViewController alloc]init];
        [self pushController:contextMessageVC];

    }else if (indexPath.row==3)
    {
        DicuessMessageViewController *dicuessMessVC=[[DicuessMessageViewController alloc]init];
        [self pushController:dicuessMessVC];
    }else if (indexPath.row==4){

        EmergencySignViewController *emergencyVC=[[EmergencySignViewController alloc]init];
        [self pushController:emergencyVC];
    }
}



@end
